package com.chenjj.java.test.sunline;



import cn.hutool.json.JSONUtil;
import cn.sunline.framework.le.Base64;
import cn.sunline.framework.le.IcenInfo;
import cn.sunline.framework.le.ck.IcenUtil;
import org.junit.Test;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;

public class CheckLicense {

    @Test
    public  void testGet(){
        String dirPath = CheckLicense.class.getResource("/").getPath();
        IcenInfo icenInfo= IcenUtil.getIcenInfo(new File(dirPath));
        System.out.println(icenInfo.getIp());
        System.out.println(icenInfo.isChecked());
    }

    @Test
    public  void testCheck(){
        String dirPath = CheckLicense.class.getResource("/").getPath();
        IcenInfo icenInfo= IcenUtil.check(new File(dirPath));

        System.out.println(dirPath);
        System.out.println(icenInfo.getIp());
        System.out.println(icenInfo.isChecked());
        System.out.println(JSONUtil.parse(icenInfo).toJSONString(1));
    }

    @Test
    public  void testCheck1(){
//        String fileName = "D:\\Code\\Gitee\\java\\src\\test\\resources\\SUNLINE-License.xml";
//        /*     */
//        /* 814 */     Icen a = getInstance();
//        /* 815 */     IcenInfo info = a.check(new File(fileName));
//        /* 816 */     System.out.println(info.getModules());
//        /* 817 */     System.out.println(info.isChecked());
    }

    @Test
    public  void testRSA() throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException, UnsupportedEncodingException {
        byte[] publicKey = { 77, 70, 119, 119, 68, 81, 89, 74, 75, 111, 90, 73, 104, 118, 99, 78, 65, 81, 69, 66, 66, 81, 65, 68, 83, 119, 65, 119, 83, 65, 74, 66, 65, 74, 104, 101, 50, 119, 86, 83, 57, 48, 122, 117, 43, 84, 68, 71, 97, 43, 53, 98, 112, 113, 56, 119, 113, 99, 105, 86, 116, 107, 51, 81, 101, 97, 79, 118, 85, 111, 103, 102, 90, 76, 120, 79, 13, 10, 114, 121, 68, 88, 56, 75, 84, 57, 115, 116, 79, 74, 57, 65, 74, 74, 56, 84, 69, 97, 51, 118, 49, 56, 76, 74, 65, 108, 86, 66, 65, 49, 87, 53, 106, 102, 111, 104, 105, 118, 69, 70, 107, 67, 65, 119, 69, 65, 65, 81, 61, 61 };
        String sign= "LqGwG92pCJtfoJA5lM05vq4EVHPqx1p/4TZXibALqj6AgXN/vMvIIoGjIf4ROH3Ww0UnYIr2n8In\nM88YS6sVcA==";
        String plainText ="ip=any-expiration=2021-11-05-mac=-project=figtree";



        byte [] publickByte = Base64.decode(publicKey);

        System.out.println(Base64.encodeToString(publickByte));

        X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(publickByte);

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        PublicKey pubKey = keyFactory.generatePublic(bobPubKeySpec);

        byte[] signed = Base64.decode(sign);
        //System.out.println(Base64.encodeToString(signed));

        Signature signatureChecker = Signature.getInstance("MD5withRSA");
        signatureChecker.initVerify(pubKey);
        signatureChecker.update(plainText.getBytes());

        if (signatureChecker.verify(signed)) {
            System.out.println("success");
        }else {
            System.out.println("failed");
        }
    }
}
